package io.gitee.hchaojie.day03.exam;

import org.junit.Assert;
import org.junit.Test;

public class ArrayRotate {
    @Test
    public void testRotateRight() {
        int[][] scores = {
                {11, 12, 13, 14},
                {21, 22, 23, 24},
                {31, 32, 33, 34}
        };

        int[][] expected = {
                {31, 21, 11},
                {32, 22, 12},
                {33, 23, 13},
                {34, 24, 14},
        };

        int[][] result = rotateRight(scores);

        for (int i = 0; i < expected.length; i++) {
            Assert.assertArrayEquals(expected[i], result[i]);
        }
    }

    public int[][] rotateRight(int[][] old) {
        int row = old[0].length;
        int col = old.length;

        int[][] now = new int[row][col];

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                now[i][j] = old[col - 1 - j][i];
            }
        }


        return now;
    }

    @Test
    public void testRotateLeft() {
        int[][] scores = {
                {11, 12, 13, 14},
                {21, 22, 23, 24},
                {31, 32, 33, 34}
        };

        int[][] expected = {
                {14, 24, 34},
                {13, 23, 33},
                {12, 22, 32},
                {11, 21, 31},
        };

        int[][] result = rotateLeft(scores);

        for (int i = 0; i < expected.length; i++) {
            Assert.assertArrayEquals(expected[i], result[i]);
        }
    }

    public int[][] rotateLeft(int[][] old) {
        int row = old[0].length;
        int col = old.length;

        int[][] now = new int[row][col];

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                now[i][j] = old[j][row - 1 - i];
            }
        }


        return now;
    }
}
